package cn.yunyichina.provider.framework.dubbo.core.filter;

import cn.yunyichina.provider.framework.dubbo.core.context.TraceContext;
import cn.yunyichina.utils.commons.IDUtils;
import cn.yunyichina.utils.commons.StringUtils;
import com.alibaba.dubbo.rpc.*;
import java.util.Map;


/**
 * Created by kevin on 2017/11/30.
 */
public class TraceConsumerFilter implements Filter  {

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        if(StringUtils.isBlank(TraceContext.getTraceId())){
            TraceContext.clear();
            String id= IDUtils.getRandomUUID32();
            TraceContext.setTraceId(id);
        }

        //埋点调用链Id
        String traceId=TraceContext.getTraceId();
        Map<String, String> attaches = invocation.getAttachments();
        attaches.put(TraceContext.TRACE_ID_KEY, traceId);

        return invoker.invoke(invocation);
    }
}
